<@>layout("/common/default.html"){
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">首页</a>
        <span lay-separator="">/</span>
        <a href="">用户权限</a>
        <span lay-separator="">/</span>
        <a><cite>角色管理</cite></a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i>
    </a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so">
            <button class="layui-btn" onclick="x_admin_show('添加用户','${ctxPath}/userpermission/user/add',600,400)"><i
                    class="layui-icon"></i>添加
            </button>
        </div>
    </div>
    <div class="layui-row layui-col-space15">
        <table class="layui-table"
               lay-data="{id:'user-list', url:'${ctxPath}/ajax///list', page:true,request: {pageName: 'page' ,limitName: 'rows'} }"
               lay-filter="user-list">
            <thead>
            <tr>
                <th lay-data="{field:'index', width:80, templet: '#table-index'}">序号</th>
                <th lay-data="{field:'username', width:150, sort:true}">用户账号</th>
                <th lay-data="{field:'name', width:200}">用户名称</th>
                <th lay-data="{field:'state', width:80,sort: true, templet: '#userz-state'}">状态</th>
                <th lay-data="{field:'createTimeStr', width:280, sort:true}">创建时间</th>
                <th lay-data="{field:'createUserName', width:200}">创建人</th>
                <th lay-data="{field:'id', toolbar: '#toolbar',fixed:'right',width:520}">操作</th>
            </tr>
            </thead>
        </table>
    </div>

    <script type="text/html" id="toolbar">

        <a class="layui-btn layui-btn-xs" lay-event="edit">详情编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="role">分配角色</a>
        <a class="layui-btn layui-btn-xs" lay-event="role_temp">分配临时角色</a>
        <a class="layui-btn layui-btn-xs" lay-event="menu_temp">分配临时权限</a>
        <a class="layui-btn layui-btn-xs" lay-event="resetPassword">重置密码</a>

        {{#  if(d.state == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">禁用</a>
        {{#  } }}
        {{#  if(d.state == 0){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="recover">恢复</a>
        {{#  } }}
    </script>

    <script type="text/html" id="userz-state">
        {{#  if(d.state == 1){ }}
        <span class="layui-badge layui-bg-green">有效</span>
        {{#  } }}
        {{#  if(d.state == 0){ }}
        <span class="layui-badge">无效</span>
        {{#  } }}
    </script>

    <script type="text/html" id="table-index">
        {{d.LAY_TABLE_INDEX + 1}}
    </script>
</div>
<script>
    $(function () {
        layui.use('table', function () {
            var url = '${ctxPath}/ajax/userpermission/user';
            var table = layui.table;
            table.on('tool(user-list)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

                //禁用人员
                if (layEvent === 'del') {
                    layer.confirm('确定禁用人员【' + data.name + '】', function () {
                        //向服务端发送删除指令
                        $.cajax('${ctxPath}/ajax/userpermission/user/del', {
                            'id': data.id
                        }, function (r) {
                            var code = r.code;
                            var msg = r.msg;
                            if (code == '1') {
                                $.msg(msg, function () {
                                    doSearch();
                                });
                            } else {
                                $.msg(msg, function () {
                                }, 2);
                            }
                        })
                    });
                }

                //恢复人员
                if (layEvent === 'recover') {
                    layer.confirm('确定恢复人员【' + data.name + '】', function () {
                        //向服务端发送删除指令
                        $.cajax('${ctxPath}/ajax/userpermission/user/recover', {
                            'id': data.id
                        }, function (r) {
                            var code = r.code;
                            var msg = r.msg;
                            if (code == '1') {
                                $.msg(msg, function () {
                                    doSearch();
                                });
                            } else {
                                $.msg(msg, function () {
                                }, 2);
                            }
                        })
                    });
                }

                //重置密码
                if (layEvent === 'resetPassword') {
                    layer.confirm('确定重置人员【' + data.name + data.username + '的密码】', function () {
                        //向服务端发送删除指令
                        $.cajax(url + '/resetPassword', {
                            'id': data.id,
                            'username': data.username,
                        }, function (r) {
                            var code = r.code;
                            var msg = r.msg;
                            if (code == '1') {
                                $.msg(msg, function () {
                                    doSearch();
                                });
                            } else {
                                $.msg(msg, function () {
                                }, 2);
                            }
                        })
                    });
                }
                if (layEvent === 'edit') {//编辑
                    x_admin_show('添加用户', '${ctxPath}/userpermission/user/edit?id=' + data.id, 600, 400)
                }
                if (layEvent === 'role') {//分配角色
                    x_admin_show('分配角色', '${ctxPath}/userpermission/user/userrole?id=' + data.id, 800, 400)
                }
                if (layEvent === 'role_temp') {//分配角色
                    x_admin_show('分配临时角色', '${ctxPath}/userpermission/user/userroletemp?id=' + data.id, 800, 420)
                }
                if (layEvent === 'menu_temp') {//分配角色
                    x_admin_show('分配临时权限', '${ctxPath}/userpermission/user/usermenutemp?id=' + data.id, 800, 600)
                }
            });
        });
    });

    //搜索
    function doSearch() {
        var state = $('#state').find('option:selected').val();
        state = state.length > 1 ? '' : state;
        layui.use('table', function () {
            var table = layui.table;
            table.reload('user-list', {
                url: '${ctxPath}/ajax/userpermission/user/list',
                where: {
                    username: $('#username').val(),
                    name: $('#name').val(),
                    state: state
                }//设定异步数据接口的额外参数
            });
        });
    }
</script>
<@>}